package org.sqlproc.sample.tutorial.model;

import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.QueryHint;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.joda.time.LocalDate;
import org.joda.time.Period;

/**
 *
 * Entity representing Person. This class is responsible for the domain object
 * related business logic for Person. Properties and associations are
 * implemented in the generated base class
 * {@link org.fornax.cartridges.sculptor.examples.library.person.domain.PersonBase}
 * .
 */
@Entity(name = "Person")
@Table(name = "PERSON", uniqueConstraints = @UniqueConstraint(columnNames = {"SSN_NUMBER", "SSN_COUNTRY" }))
@NamedQueries(
		@NamedQuery(
				name = "findAll",
				query = "select person FROM Person person",
				hints = { @QueryHint(name = "org.hibernate.cacheable", value = "true"),
						  @QueryHint(name = "org.hibernate.cacheRegion", value = "query.Person")}))
public class Person extends PersonBase {
	public Person() {
	}

	public Person(Gender sex, Ssn ssn) {
		super(sex, ssn);
	}

	public Integer getAge() {
		LocalDate birth = getBirthDate();
		if (birth == null) {
			return null;
		}
		LocalDate today = new LocalDate();
		Period period = new Period(birth, today);
		return period.getYears();
	}
}
